<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/27 0027
 * Time: 22:06
 */

namespace app\admin\controller;


use app\Lib\Api\Update\delFile;
use app\Lib\Api\Update\updateImg;
use app\Lib\TopClient;
use think\Controller;
use think\Image;

class Upload extends Base
{
    public function img(){
        if($this->request->isPost()){

        }
    }


    public function uploadImg(){
        try{
            $file = request()->file('file');
            if($file) {
                $filePath = ROOT_PATH . 'html' . DS . 'uploads';
                $info = $file->move($filePath);
                if ($info) {
                    $filePathImg = $filePath . DS . $info->getSaveName();
                    Image::open($filePathImg)->thumb(1000, 1000)->save($filePathImg);
                    $model = new updateImg();
                    $model->setArgs('opUid', $this->login_uid);
                    $model->setArgs('filePath',DS . 'uploads' . DS . $info->getSaveName());
                    $model->setArgs('fileName', $info->getFilename());
                    $result = (new TopClient($model))->getSuccessData();
                    return ['code' => 0,'img'=>$result];
                } else {
                    return ['code' => 1,'msg'=>'上传失败'];
                }
            }
        }catch (\Exception $e){
//            unlink(DS.'html' . DS . 'uploads'. DS . $info->getSaveName());
            return ['code' => 1,'msg'=>$e->getMessage()];
        }

    }


    public function notThumbnail(){
        try{
            $file = request()->file('file');
            if($file) {
                $filePath = ROOT_PATH . 'html' . DS . 'uploads';
                $info = $file->move($filePath);
                if ($info) {
//                    $filePathImg = $filePath . DS . $info->getSaveName();
//                    Image::open($filePathImg)->thumb(500, 500)->save($filePathImg);
                    $model = new updateImg();
                    $model->setArgs('opUid', $this->login_uid);
                    $model->setArgs('filePath',DS . 'uploads' . DS . $info->getSaveName());
                    $model->setArgs('fileName', $info->getFilename());
                    $result = (new TopClient($model))->getSuccessData();
                    return ['code' => 0,'img'=>$result];
                } else {
                    return ['code' => 1,'msg'=>'上传失败'];
                }
            }
        }catch (\Exception $e){
//            unlink(DS.'html' . DS . 'uploads'. DS . $info->getSaveName());
            return ['code' => 1,'msg'=>$e->getMessage()];
        }

    }


    public function delFile(){
        try{
            if($this->request->isPost()){

//                $model =  new delFile();
//                $model->setArgs('fileId',input('imgId'));
//                $result = (new TopClient($model))->getSuccessData();
//                unlink('..\html'.$result);
                return ['code' => 1,'msg'=>'删除成功'];
            }
        }catch (\Exception $e){
//            unlink(DS.'html' . DS . 'uploads'. DS . $info->getSaveName());
            return ['code' =>-1,'msg'=>$e->getMessage()];
        }
    }
}